package com.hzau.tourist_service_sys.ultils;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.SignatureException;

import java.util.Date;
import java.util.HashMap;

public class jjwttool {
    private jjwttool(){};
    public static String genJwt(int id ,int tag){
        HashMap<String,Object> hashMap=new HashMap<>();
        hashMap.put("id",id);
        hashMap.put("tag",tag);
        String jwt= Jwts.builder().setClaims(hashMap)
                .signWith(SignatureAlgorithm.HS256,"tourist_service")
                .setExpiration(new Date(System.currentTimeMillis()+3600*1000*8))
                .compact();
        return jwt;
    }
    public  static Integer getUserId(String jwt){
        Integer id=(Integer) Jwts.parser().setSigningKey("tourist_service")
                .parseClaimsJws(jwt)
                .getBody().get("id");
        System.out.println(id);
        return id;

    }
    public static  boolean testJwts(String jwt){

       try{ Jwts.parser().setSigningKey("tourist_service")
                .parseClaimsJws(jwt)
                .getBody();
       return true;}
       catch (MalformedJwtException e ){
           return  false;
       }
       catch (SignatureException e){
           return  false;
       }
    }
}
